<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<title>物料申请</title>
		<meta http-equiv="expires" content="0">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<link rel="stylesheet" href="../../../css/reset.css" />
		<link rel="stylesheet" href="../../../css/mui.min.css" />
		<link rel="stylesheet" href="../../../css/iconfont.css" />
		<link rel="stylesheet" href="../../../css/cmms.css" />
		<link rel="stylesheet" href="../../../css/sale/materiel/materielApply.css" />
	</head>
	<body>
		<div id="materielApply" class="mui-content center"  v-cloak>
			<div id="materielApplyIndex" v-if="show1">
				<div class="selectHospital" @tap="selectHospital">
					<span>选择医院</span><span ><input type="text" unselectable="on" v-model="hospital" placeholder="请选择医院"/><i class="iconfont icon-chakangengduo"></i></span>
				</div>
				<div class="materielBox">
					<ul v-for="(materielBoxs,index) in materielBoxList">
						<li><span>物料名称</span><span @tap="selectMateriel(index)"><input type="text" readonly unselectable="on" v-model="materielBoxs.materiel" placeholder="请填选择物料"/><i class="iconfont icon-chakangengduo"></i></span></li>
						<li><span>数量</span><span><input type="text" v-model="materielBoxs.materielCount" placeholder="请填写数量"/></span></li>
					</ul>
					<p v-if="webName=='apply'"><span @tap="addMateriel"><i class="iconfont icon-jiahao"></i>添加物料</span></p>	
				</div>
				<div class="remark">
					<textarea name="" v-model="applyReason" placeholder="请填写申请理由"></textarea>
				</div>
				<div class="buttonBox">
					<button class="submit-btn" @tap="submitApply">提交</button>
				</div>
			</div>
			<div id="selectHospital"  v-if="show2">
				<!--搜素框-->
				<div class="searchTop">
					<span class=" mui-icon mui-icon-search"></span>
	    			<input v-model="hospital" type="text" class="mui-input-clear" placeholder="请输入医院名称">
	    			<button @tap="searchHospital" class="btn-primary">搜索</button>
				</div>
				<!--列表-->
				<ul class="ulStyle" v-if="ulShow">
				    <li @tap="jumpHospitalDetail(item.hospitalId,item.hospitalName)" v-for="item in hospitalItems">{{item.hospitalName}}</li>
				</ul>
				<div>
					<button @tap="clearItem" class="clearBtn">清除搜素记录</button>
				</div>
			</div>
			<div id="selectMateriel" v-if="show3">
				<!--搜素框-->
				<div class="searchTop">
					<span class=" mui-icon mui-icon-search"></span>
	    			<input v-model="materielName" type="text" class="mui-input-clear" placeholder="请输入物料名称">
	    			<button @tap="searchMateriel" class="btn-primary">搜索</button>
				</div>
				<!--列表-->
				<ul class="ulStyle" v-if="ulShow">
				    <li @tap="jumpMaterielDetail(item.id,item.name)" v-for="item in materielItems">{{item.name}}</li>
				</ul>
				<div>
					<button @tap="clearItem" class="clearBtn">清除搜素记录</button>
				</div>
			</div>
		</div>
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/vue.js"></script>
		<script src="../../../js/cmms.js?time=333"></script>
		<script>
			new Vue({
				el:"#materielApply",
				data(){
					return{
						arrIndex:0,
						info:[],
//						doctorName:'',
						hospital:'',//医院
						materielName:'',//物料
						ulShow:false,
						nameArr:[],
						idArr:[],
						materielBoxList:[
							{
						      materiel:'',
						      materielCount:'',
						      materielId:0
						   	}
						],//物料项目集合
						materielBox:{
							materiel:'',
						    materielCount:'',
						    materielId:0
					   	},
						materielList:[],//物料项目集合
						materiels:{
					      applyNum: 0,
					      consumablelId: 0
					  	},
						isBtnShow:true,
						materielItems:[],
						hospitalItems:[],
						show1:true,
						show2:false,
						show3:false,
						index:'',
						applyReason:'',//申请理由
						partnerHospitalId:0,
						editId:0,
						webName:''
					}
				},
				mounted(){
//					var vConsole = new VConsole();
					this.getUrl();
					this.init();
//					console.log(this.materielList);
				},
				methods:{
					getUrl(){
						let _this=this;
						var url = location.search; //获取url中"?"符后的字串    
						var theRequest = new Object();  
//						console.log(url);
					   	if (url.indexOf("?") != -1) {   
					      var str = url.substr(1);  
//					      console.log(str);
					      strs = str.split("&");  
					      for(var i = 0; i < strs.length; i ++) {   
					         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);   
					      } 
					   	}   
//					   	console.log(theRequest);
					   	_this.webName = theRequest.webName;
					   	_this.id = parseInt(theRequest.id);
//					   	console.log(_this.id);
						console.log(_this.webName);
					},
					init(){
						let _this=this;
						/*重新申请*/
						if(_this.webName=='editApply'){
							//根据id查询耗材申请
							AT.muiAjax('/partner-manager/consumableApply/selectConsumableApplyById','post',{
								 id:_this.id
							},function(realData){
								console.log(realData);
								_this.applyReason = realData.applyReason;
								_this.hospital = realData.partnerHospitalName;
								_this.editId = realData.id;
								_this.materielBoxList=[];
								for(let i=0;i<realData.list.length;i++){
									_this.materielBoxList.push({});
									_this.materielBoxList[i].materielCount = realData.list[i].applyNum;
									_this.materielBoxList[i].materiel = realData.list[i].consumablelName;
									_this.materielBoxList[i].materielId = realData.list[i].consumablelId;
									_this.materielList.push({});
									_this.materielList[i].consumablelId = realData.list[i].consumablelId;
									_this.materielList[i].applyNum = realData.list[i].applyNum;
//									_this.materielList[i].id = realData.list[i].id;
									_this.nameArr.push(realData.list[i].consumablelName);
									_this.idArr.push(realData.list[i].consumablelId);
								}
								console.log(_this.materielList);
								console.log(_this.materielBoxList);
								console.log(_this.nameArr);
								console.log(_this.idArr);
							});
						}else if(_this.webName=='apply'){
							
						}
					},
					/*申请页*/
					apply(){
						
					},
					/*选择医院*/
					selectHospital(){
						this.show1 = false;
						this.show2 = true;
						this.show3 = false;
						this.ulShow = false;
						this.hospital = '';
						this.searchHospital();
					},
					//医院搜索
					searchHospital(){
						let _this = this;
						AT.muiAjax('/partner-sale/apply/getPartnerHospital','post',{
							 name:_this.hospital
						},function(realData){
							_this.ulShow = true;
							_this.hospitalItems = realData;
//							console.log(realData);
						});
					},
					jumpHospitalDetail(id,name){
						this.show3 = false;
						this.show2 = false;
						this.show1 = true;
						this.hospital = name;
						this.partnerHospitalId = id;
//						console.log(this.hospital);
					},
					/*选择物料*/
					selectMateriel(index){
						console.log(index);
						this.arrIndex = index;
						this.show1 = false;
						this.show2 = false;
						this.show3 = true;
						
						this.materielItems = [];
						this.searchMateriel();
 					},
					jumpMaterielDetail(id,name){
//						console.log(this.materielBoxList);
						console.log(this.materielList);
						console.log(this.nameArr);
						console.log(this.idArr);
						this.show3 = false;
						this.show2 = false;
						this.show1 = true;
//						console.log(this.arrIndex);
						let obj = {
							applyNum:0,
					      	consumablelId:0
						};
						if(this.nameArr.length<this.arrIndex+1){
							this.nameArr.push(name);
							this.idArr.push(id);
							this.materielList.push(obj);
						}else{
							this.nameArr[this.arrIndex] = name;
							this.idArr[this.arrIndex] = id;
							let materIndex = this.materielList.findIndex(target=>target.MateriaId==id);
					        console.log(materIndex);
					        if(materIndex!=-1){
					          this.materielList[materIndex] = obj;
					        }
						}
						if(this.webName=='editApply'){
							
						}else if(this.webName=='apply'){
							
							
						}
						console.log(this.materielBoxList);
						console.log(this.materielList);
						for(let i=0;i<this.materielBoxList.length;i++){
							this.materielBoxList[i].materiel = this.nameArr[i];
							this.materielList[i].consumablelId = parseInt(this.idArr[i]);
						}
						console.log(this.nameArr);
						console.log(this.idArr);
						console.log(this.materielList);
					},
					//物料搜索
					searchMateriel(){
						let _this = this;
						AT.muiAjax('/partner-sale/apply/getPartnerConsumable','post',{
							 name:_this.materielName
						},function(realData){
							_this.ulShow = true;
							_this.materielItems = realData;
//							console.log(realData);
						});
					},
					addMateriel(){
						let _this = this;
//						console.log(this.nameArr);
//						console.log(this.idArr);
						console.log(_this.materielBox);
//						console.log(_this.arrIndex+1);
//						console.log(_this.materielBoxList);
						_this.materielBox = {
							materiel:'',
						    materielCount:'',
						    materielId:0
					   	};
						if(_this.materielBoxList[_this.arrIndex].materielCount==''){
							mui.alert("物料名称不能为空！");
							return false;
						}else if(_this.materielBoxList[_this.arrIndex].materielCount==null){
							mui.alert("数量不能为空！");
							return false;
						}else{
							_this.materielBoxList.push(_this.materielBox);
							_this.arrIndex++;
						}
						console.log(_this.materielBoxList);
						console.log(_this.materielList);
					},
					/*提交申请*/
					submitApply(){
						let _this = this;
//						console.log(_this.arrIndex);
//						console.log(_this.materielBoxList);
						console.log(_this.materielList);
						for(let i=0;i<_this.materielBoxList.length;i++){
//							_this.materielList.push(obj);
							_this.materielList[i].applyNum = parseInt(_this.materielBoxList[i].materielCount);
							
//							console.log(_this.materielList[i]);
							if(_this.materielBoxList[i].materiel==''){
								mui.alert("物料名称不能为空！");
								return false;
								break;
							}else if(_this.materielBoxList[i].materielCount==''){
								mui.alert("数量不能为空！");
								return false;
								break;
							}
//							console.log(_this.materielList[i].applyNum);
//							console.log(parseInt(_this.materielBoxList[i].materielCount));
						}
						console.log(_this.materielList);
//						console.log(_this.materielBoxList);
						console.log(_this.hospital);
						if(_this.hospital==''){
							mui.alert("请选择医院名称！");
							return false;
						}else{
//							alert(222);
							if(_this.webName=='editApply'){
								//修改需要传id
								console.log(_this.materielList);
								console.log(_this.partnerHospitalId);
								AT.muiAjax('/partner-sale/apply/updateConsumableApply','post',
								{	
									id:_this.editId,
									applyReason:_this.applyReason,
									list:_this.materielList,
									partnerHospitalId:_this.partnerHospitalId
								},function(realData){
									console.log(realData);
									location.href="materiel.html";
								});
							}else if(_this.webName=='apply'){
								AT.muiAjax('/partner-sale/apply/saveSaleConsumableApply','post',
								{	
									applyReason:_this.applyReason,
									list:_this.materielList,
									partnerHospitalId:_this.partnerHospitalId
								},function(realData){
									console.log(realData);
									location.href="materiel.html";
								});
							}
						}
					},
					clearItem(){
						
					}
				}	
			})
		</script>
	</body>
</html>

